Imports Microsoft.VisualBasic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports EntityLayer

Public Class OrdenCompraDA

    Public Shared Function ListarOrdenes() As DataSet
        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myDataadapter As New SqlDataAdapter("usp_ListOrdenesCompra", myConnection)
        myDataadapter.SelectCommand.CommandType = CommandType.StoredProcedure

        Dim ds As New DataSet

        myDataadapter.Fill(ds, "Ordenes")

        Return ds

    End Function

    Public Shared Function ObtenerNuevoCodigo() As Integer

        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myCommand As New SqlCommand("usp_ListNuevoCodigoOrdenCompra", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure

        Dim para As SqlParameter

        para = New SqlParameter("@intCodOrden", SqlDbType.Int)
        para.Direction = ParameterDirection.Output
        myCommand.Parameters.Add(para)

        myConnection.Open()

        myCommand.ExecuteNonQuery()

        myConnection.Close()

        Return myCommand.Parameters("@intCodOrden").Value

    End Function

    Public Shared Function Agregar(ByVal OrdenCompraEN As OrdenCompraE) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("usp_AddOrdenCompra", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@intNroOrden", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.Numero
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@dtFechaEmision", SqlDbType.DateTime)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.FechaEmision
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strPlazoEntrega", SqlDbType.VarChar, 100)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.PlazoEntrega
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@flSubTotal", SqlDbType.Decimal)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.Subtotal
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@flIGV", SqlDbType.Decimal)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.IGV
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@flTotal", SqlDbType.Decimal)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.Total
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strFormaPago", SqlDbType.VarChar, 50)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.FormaPago
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strArea", SqlDbType.VarChar, 100)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.Area
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strCodigo", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.Codigo
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@intNroCoti", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.NumeroCoti
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strNroCuenta", SqlDbType.VarChar, 50)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.NumeroCuenta
            myCommand.Parameters.Add(para)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True
        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function

    Public Shared Function Actualizar(ByVal OrdenCompraEN As OrdenCompraE) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("usp_UpdOrdenCompra", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@intNroOrden", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.Numero
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@dtFechaEmision", SqlDbType.DateTime)
            para.Direction = ParameterDirection.Input
            para.Value = OrdenCompraEN.FechaEmision
            myCommand.Parameters.Add(para)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True
        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function


    Public Shared Function Eliminar(ByVal intIdOrdenCompra As Integer) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("usp_DelOrdenCompra", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@intOrden", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = intIdOrdenCompra
            myCommand.Parameters.Add(para)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True
        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function

    Public Shared Function SeleccionarxCodigo(ByVal intIdOrden As Integer) As DataSet

        Dim cnnSql As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim daSql As New SqlDataAdapter("usp_ListOrdenCompraByCodigo", cnnSql)
        daSql.SelectCommand.CommandType = CommandType.StoredProcedure

        Dim dsDatos As New DataSet

        daSql.SelectCommand.Parameters.Add("@intIdOrden", SqlDbType.Int).Value = intIdOrden

        daSql.Fill(dsDatos, "OrdenCompra")

        Return dsDatos

    End Function

    Public Shared Function ListarTodo() As DataSet
        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myDataadapter As New SqlDataAdapter("COM_OrdenCompra_ListAll", myConnection)
        myDataadapter.SelectCommand.CommandType = CommandType.StoredProcedure

        Dim ds As New DataSet

        myDataadapter.Fill(ds, "Ordenes")

        Return ds

    End Function


End Class
